///////////////////////////////////////////////////////////////
//** INDIGENOUS POLITICAL PARTICIPATION: VARIABLE CREATION **//
///////////////////////////////////////////////////////////////

// NOTES // 
* LAPOP data access: http://datasets.americasbarometer.org/database/
	** Download Grand Merge File after logging in as a registered or free user.
* Script last updated February 2025 

// DEFINING SPATIAL & TEMPORAL SCOPE //

* Drop non-Latin American countries. 
	* Guayana
	drop if pais==24
	* Surinam
	drop if pais==27
	* Trinidad and Tobago
	drop if pais==25
	* Haiti
	drop if pais==22
	* EEUU
	drop if pais==40
	* Jamaica
	drop if pais==23
	* Canada
	drop if pais==41
	* Belize
	drop if pais==26
	* Bahamas
	drop if pais==28
	* Barbados 
	drop if pais==29
	* Grenada
	drop if pais==30
	* St Lucia
	drop if pais==31
	* Dominica
	drop if pais==32
	* St Vincent & Grenadine
	drop if pais==34
	* St Kitts & Nevis
	drop if pais==35	
	
* Drop Dominican Republic b/c it does not include ethnicity question:
drop if pais == 21	

* Limit analysis to 2004-2019 for which LAPOP survey data was available during the writing of the manuscript.
keep if wave >= 2004 & wave <= 2018

// POLITICAL PARTICIPATION VARIABLES //

*** Protest

* Original protest survey questions from Lapop 
	* prot1 "Have you ever participated in a public demonstration or protest" (1=sometimes, 2= almost never, 3 = never) asked in 2004, 2006, 2008.
	* prot2 "In the last 12 months have you participated in a protest?" (1 = sometimes, 2 = almost never, 3 = never) asked in 2006, 2008.
	* prot3 "In the last 12 months have you participated in a protest? (1 = si, 2 = no) asked in 2010, 2012, 2014, 2016/17, 2018/9
	* prot4 "How many times have you protested?" asked in 2010, 2012, 2014, 2016/17.
	* prot7 "Have you participated in road blocks?" asked in 2012, 2014 in Argentina and Honduras.
	
* To create a general protest measure with the most coverage, we use prot2 and prot3 
	* generate new variables, recoded to the same scale
		** for 2006, 2008
	gen protest2 = prot2
	recode protest2 (2=1) (3=0) 
	label var protest2 "Protested in the last 12 months"
	label define protest2 0 "No" 1 "Sometimes or Rarely"
	label val protest2 protest2
		** for 2010, 2012, 2014, 2016/17, 2018/9
	gen protest3 = prot3
	recode protest3 (2=0)
	label var protest3 "Protested in the last 12 Months"
	label define protest3 0 "No" 1 "Yes" 
	label val protest3 protest3
	* generate new variable that combines prot2 and prot3 responses into one variable
	gen protest = .
	replace protest = protest2 if year==2006
	replace protest = protest2 if year==2008
	replace protest = protest3 if year==2010
	replace protest = protest3 if year==2012
	replace protest = protest3 if year==2014 
	replace protest = protest3 if year==2016
	replace protest = protest3 if year==2017
	replace protest = protest3 if year==2018
	replace protest = protest3 if year==2019
	label var protest "Protested in the last 12 Months" 
	label define protest 0 "No" 1 "Yes" 
	label val protest protest
	
*** Voting	
	
	gen vote= vb2 
	label variable vote "whether respondent voted in the last presidential election"
	recode vote (1=1) (2=0) 
	label define vote 1 "Yes" 0 "No"
	label values vote vote
	
*** Town Meeting	
	
	gen townmeeting= np1
	label variable townmeeting "[np1] attended city council or town meeting in past 12 months"
	recode townmeeting (1=1) (2=0) 
	label values townmeeting vote
	* Note: This question was asked in a limited number of countries in 2018/19 (includes Mexico, Guatemala, El Salvador, Honduras, Nicaragua, Colombia, Peru, Paraguay, Jamaica)
	
*** Petition (local)	
	
	gen petitionlocal = np2
	label variable petitionlocal "[np2] request assistance or petition municipal govt in past 12 months"
	recode petitionlocal (1=1) (2=0) 
	label values petitionlocal vote
	* Note: In 2016/17, this question was only asked in Honduras and Guayana.

*** Contact (local)	
	
	gen contactlocal= cp4a
	label variable contactlocal "[cp4a] to solve problem, ever sought help from local public official?"
	recode contactlocal (1=1) (2=0) 
	label values contactlocal vote
	* Note: in 2016/17 and 2018/19, this question was only asked in Colombia. 
	
*** Contact (federal)	
	
	gen contactfederal= cp4
	label variable contactfederal "[cp4] to solve problem, ever sought help from federal ministry or agency?"
	recode contactfederal (1=1) (2=0) 
	label values contactfederal vote
	* Note: In 2014, this question was only asked in Costa Rica. N/A in 2016/17 and 2018/19.
	
*** Contact (congress)	
	
	gen contactcongress= cp2
	label variable contactcongress "[cp2] to solve problem, ever sought help from congress/parliament?"
	recode contactcongress (1=1) (2=0) 
	label values contactcongress vote
	* Note: In 2014, this question was only asked in Costa Rica. N/A in 2016/17 and 2018/19.

*** Campaign	
	
	gen campaign= pp2
	label variable campaign "[pp2] worked for political party or campaign in last presidential election"
	recode campaign (1=1) (2=0) 
	label values campaign vote
	* Note: This question was not asked in 2014, 2016/17, and 2018/19 surveys
	
*** Party Meeting
	
	gen polparty1= cp13
	label variable polparty1 "[cp13] attended meetings of a political party or pol. org at least 1 x month (from 1-4 scale)"
	recode polparty1 (1/2=1) (3/4=0)
	label values polparty1 vote
	* Note: This question was asked in a limited number of countries in 2018/19 (includes Mexico, Guatemala, El Salvador, Honduras, Nicaragua, Colombia, Peru, Paraguay, Brazil, Jamaica)
	
// POLITICAL IDEOLOGY //

gen rightleft= l1 
recode rightleft (1/4=1 "left") (5/6=2 "middle") (7/10=3 "right"), gen (ideology)

quietly tabulate ideology, generate(ideology_)
rename ideology_1 left
rename ideology_2 middle
rename ideology_3 right

// ORGANIZATIONAL MEMBERSHIP //

gen relig=cp6 
label variable relig "participation in the meetings of a religious group"
recode relig (4=0)(3=1)(1=3) 
label define relig 0 "Nunca" 1 "Una o dos veces al ano" 2 "Una o dos veces al mes" 3 "una vez a la semana"

	gen relig01=cp6 
	label variable relig01 "any participation in the meetings of a religious group in the last year"
	recode relig01 (4=0)(3=1)(2=1) 
	label define relig01 0 "No" 1 "Yes" 

	gen religoften=relig 
	label variable religoften "attends meetings of a religious group at least once or twice a month in the last year"
	recode religoften (0=0) (1=0) (2=1) (3= 1)  
	label define religoften 0 "No" 1 "Yes" 
	
gen parentorg= cp7
label variable parentorg "participation in school parents' association"
recode parentorg  (4=0)(3=1)(1=3) 
label define parentorg 0 "Nunca" 1 "Una o dos veces al ano" 2 "Una o dos veces al mes" 3 "una vez a la semana"

	gen parentorg01= cp7
	label variable parentorg01 "any participation in school parents' association in the last year"
	recode parentorg01 (4=0)(3=1)(2=1) (1=1)
	label define parentorg01 0 "No" 1 "Yes" 
	
	gen parentoften=parentorg
	label variable parentoften "attends meetings of a parent group at least once or twice a month in the last year"
	recode parentoften (0=0) (1=0) (2=1) (3= 1)  
	label define parentoften 0 "No" 1 "Yes" 

gen comm=cp8
label variable comm "participation in a community organization"
recode comm (4=0)(3=1)(1=3) 
label define comm 0 "Nunca" 1 "Una o dos veces al ano" 2 "Una o dos veces al mes" 3 "una vez a la semana"

	gen comm01=cp8
	label variable comm01 "any participation in a community organization in the last year"
	recode comm01  (4=0)(3=1)(2=1) (1=1)
	label define comm01  0 "No" 1 "Yes"
	
	gen commoften=comm
	label variable commoften "attends meetings of a community group at least once or twice a month in the last year"
	recode commoften (0=0) (1=0) (2=1) (3= 1)  
	label define commoften 0 "No" 1 "Yes" 

gen prof=cp9 
label variable prof "participation in a professional or commercial organization"
recode prof (4=0)(3=1)(1=3) 
label define prof 0 "Nunca" 1 "Una o dos veces al ano" 2 "Una o dos veces al mes" 3 "una vez a la semana"

	gen prof01=cp9 
	label variable prof01 "any participation in a professional or commercial organization in the last year"
	recode prof01 (4=0)(3=1)(2=1) (1=1)
	label define prof01 0 "No" 1 "Yes"
	
	gen profoften = prof
	label variable profoften "attends meetings of a professional group at least once or twice a month in the last year"
	recode profoften (0=0) (1=0) (2=1) (3= 1)  
	label define profoften 0 "No" 1 "Yes" 
	
gen women=cp20
label variable women "participation in a womens' organization"
recode women (4=0)(3=1)(1=3) 
label define women 0 "Nunca" 1 "Una o dos veces al ano" 2 "Una o dos veces al mes" 3 "una vez a la semana"

	gen women01=cp20
	label variable women01 "any participation in a professional or commercial organization in the last year"
	recode women01 (4=0)(3=1)(2=1) (1=1)
	label define women01 0 "No" 1 "Yes"
	
	gen womenoften = women
	label variable womenoften "attends meetings of a professional group at least once or twice a month in the last year"
	recode womenoften (0=0) (1=0) (2=1) (3= 1)  
	label define womenoften 0 "No" 1 "Yes" 

gen polparty=cp13
label variable polparty "participation in a political movement or party"
recode polparty (4=0)(3=1)(1=3) 
label define polparty 0 "Nunca" 1 "Una o dos veces al ano" 2 "Una o dos veces al mes" 3 "una vez a la semana"

	gen polparty01=cp13
	label variable polparty01 "any participation in a political movement or party in the last year"
	recode polparty01 (4=0)(3=1)(2=1) (1=1)
	label define polparty01 0 "No" 1 "Yes"
	
* Union Variable: only available for 2004 2006 2008

gen union=cp10
label variable union "how often did you attend a meeting of a labor union (sindicato)"
recode union (4=0)(3=1) (2=2) (1=3) (else=.)
label define union 0 "Nunca" 1 "Una o dos veces al ano" 2 "Una o dos veces al mes" 3 "una vez a la semana"
label value union union

	gen union01=cp10
	label variable union01 "any participation in a labor union in the last year"
	recode union01  (4=0)(3=1)(2=1) (1=1) (else=.)
	label define union01 0 "No" 1 "Yes"
	label value union01 union01
	
	gen unionoften = union
	label variable unionoften "attends meetings of a labor union at least once or twice a month in the last year"
	recode unionoften (0=0) (1=0) (2=1) (3= 1) (else=.)
	label define unionoften 0 "No" 1 "Yes" 
	label value unionoften unionoften

	
* Need to standardize missing values in membership vars

replace relig=. if relig==.a	
replace relig=. if relig==.b
replace relig=. if relig==.c
replace relig=. if relig==.z	

replace parentorg=. if parentorg==.a	
replace parentorg=. if parentorg==.b
replace parentorg=. if parentorg==.c
replace parentorg=. if parentorg==.z

replace comm=. if comm==.a	
replace comm=. if comm==.b
replace comm=. if comm==.c
replace comm=. if comm==.z

replace prof=. if prof==.a	
replace prof=. if prof==.b
replace prof=. if prof==.c
replace prof=. if prof==.z	

replace women=. if women==.a	
replace women=. if women==.b
replace women=. if women==.c
replace women=. if women==.z	


* Generate the final participation in a community organization variable
gen orgmtg = .
replace orgmtg = 0 if relig==0|. & parentorg==0|. & comm== 0|. & prof == 0|. & year==2004
replace orgmtg = 0 if relig==0|. & parentorg==0|. & comm== 0|. & prof == 0|. & year==2006
replace orgmtg = 0 if relig==0|. & parentorg==0|. & comm== 0|. & prof == 0|. & women== 0|. & year==2008
replace orgmtg = 0 if relig==0|. & parentorg==0|. & comm== 0|. & prof == 0|. & women== 0|. & year==2010
replace orgmtg = 0 if relig==0|. & parentorg==0|. & comm== 0|. & prof == 0|. & women== 0|. & year==2012
replace orgmtg = 0 if relig==0|. & parentorg==0|. & comm== 0|. & prof == 0|. & women== 0|. & year==2014
replace orgmtg = 1 if relig==1|parentorg==1|comm==1 |prof ==1|women==1
replace orgmtg = 2 if relig==2|parentorg==2|comm==2 |prof ==2|women==2
replace orgmtg = 3 if relig==3|parentorg==3|comm==3 |prof ==3|women==3

replace orgmtg = . if relig==. & parentorg==. & comm==. & prof==. & women==.

label var orgmtg "attends either a relig, parent, commm, prof or women group" 
label define orgmtg 0 "Nunca" 1 "Una o dos veces al ano" 2 "Una o dos veces al mes" 3 "una vez a la semana"
label values orgmtg orgmtg orgmtg orgmtg

gen orgmtg01=orgmtg
recode orgmtg01 (1/3=1) (0=0)

label var orgmtg01 "any participation in relig, parent, commm, prof or women group in the last year" 
label define orgmtg01 0 "No" 1 "Yes"
label values orgmtg01 orgmtg01	

// WEALTH //

* Creating Asset-based Wealth Index "Quintall" (Note: Thanks to Abby Cordova for the code, and the idea) 

ssc install sumdist, all replace
set more off
qui gen quintall=.
qui gen wealthscores=.
qui levelsof pais if pais<40 & year==2004, local(K)
foreach k of local K {
qui pca    r1 r3 r4 r5 r6 r7  r12  if pais==`k' & ur==1 & year==2004
qui predict  f1`k'u if pais==`k' & ur==1  & year==2004
qui sumdist f1`k'u,  n(5) qgp(quint`k'u)
qui replace quintall=quint`k'u if pais==`k' & ur==1  & year==2004
qui replace wealthscores=f1`k'u if pais==`k' & ur==1 & year==2004

qui pca   r1 r3 r4 r5 r6 r7  r12 if pais==`k' & ur==2 & year==2004
qui predict  f1`k'r if pais==`k' & ur==2 & year==2004
qui sumdist f1`k'r,  n(5) qgp(quint`k'r)
qui replace quintall=quint`k'r if pais==`k' & ur==2 & year==2004
qui replace wealthscores=f1`k'r if pais==`k' & ur==2 & year==2004

}

drop f1*u  f1*r quint*u quint*r wealthscores
	* try different commands: 
	* macro drop _all 
	* drop f1`k'u f1`k'r quint`k'u quint`k'r wealthscores
label variable quintall " Quintiles de riqueza"


qui gen wealthscores=.
qui levelsof pais if pais<40 & year==2006, local(K)
foreach k of local K {
qui pca   r1 r3 r4 r4a r5 r6 r7  r8 r12 r14   if pais==`k' & ur==1 & year==2006
qui predict  f1`k'u if pais==`k' & ur==1 & year==2006
qui sumdist f1`k'u,  n(5) qgp(quint`k'u)
qui replace quintall=quint`k'u if pais==`k' & ur==1 & year==2006
qui replace wealthscores=f1`k'u if pais==`k' & ur==1 & year==2006

qui pca   r1 r3 r4 r4a r5 r6 r7 r8 r12 r14  if pais==`k' & ur==2 & year==2006
qui predict  f1`k'r if pais==`k' & ur==2 & year==2006
qui sumdist f1`k'r,  n(5) qgp(quint`k'r)
qui replace quintall=quint`k'r if pais==`k' & ur==2 & year==2006
qui replace wealthscores=f1`k'r if pais==`k' & ur==2 & year==2006

}

drop f1*u  f1*r quint*u quint*r wealthscores
	* macro drop _all 
	* drop f1`k'u f1`k'r quint`k'u quint`k'r wealthscores

qui gen wealthscores=.
qui levelsof pais if pais<40 & year==2008, local(K)
foreach k of local K {
qui pca   r1 r3 r4 r4a r5 r6 r7  r8 r12 r14 r15   if pais==`k' & ur==1& year==2008
qui predict  f1`k'u if pais==`k' & ur==1& year==2008
qui sumdist f1`k'u,  n(5) qgp(quint`k'u)
qui replace quintall=quint`k'u if pais==`k' & ur==1& year==2008
qui replace wealthscores=f1`k'u if pais==`k' & ur==1& year==2008

qui pca   r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15  if pais==`k' & ur==2& year==2008
qui predict  f1`k'r if pais==`k' & ur==2& year==2008
qui sumdist f1`k'r,  n(5) qgp(quint`k'r)
qui replace quintall=quint`k'r if pais==`k' & ur==2& year==2008
qui replace wealthscores=f1`k'r if pais==`k' & ur==2& year==2008

}

drop f1*u  f1*r quint*u quint*r wealthscores
* macro drop _all 
* drop f1`k'u f1`k'r quint`k'u quint`k'r wealthscores

qui gen wealthscores=.
qui levelsof pais if pais<40 & year==2010, local(K)
foreach k of local K {
qui pca   r1 r3 r4 r4a r5 r6 r7  r8 r12 r14 r15 r16   if pais==`k' & ur==1 & year==2010
qui predict  f1`k'u if pais==`k' & ur==1 & year==2010
qui sumdist f1`k'u,  n(5) qgp(quint`k'u)
qui replace quintall=quint`k'u if pais==`k' & ur==1 & year==2010
qui replace wealthscores=f1`k'u if pais==`k' & ur==1 & year==2010

qui pca   r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15 r16 if pais==`k' & ur==2 & year==2010
qui predict  f1`k'r if pais==`k' & ur==2 & year==2010
qui sumdist f1`k'r,  n(5) qgp(quint`k'r)
qui replace quintall=quint`k'r if pais==`k' & ur==2 & year==2010
qui replace wealthscores=f1`k'r if pais==`k' & ur==2 & year==2010

}

drop f1*u  f1*r quint*u quint*r wealthscores
* macro drop _all 
* drop f1`k'u f1`k'r quint`k'u quint`k'r wealthscores

qui gen wealthscores=.
qui levelsof pais if pais<40 & year==2012, local(K)
foreach k of local K {
qui pca   r1 r3 r4 r4a r5 r6 r7  r8 r12 r14 r15 r16  if pais==`k' & ur==1 & year==2012
qui predict  f1`k'u if pais==`k' & ur==1 & year==2012
qui sumdist f1`k'u,  n(5) qgp(quint`k'u)
qui replace quintall=quint`k'u if pais==`k' & ur==1 & year==2012
qui replace wealthscores=f1`k'u if pais==`k' & ur==1 & year==2012

qui pca   r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15 r16 if pais==`k' & ur==2 & year==2012
qui predict  f1`k'r if pais==`k' & ur==2 & year==2012
qui sumdist f1`k'r,  n(5) qgp(quint`k'r)
qui replace quintall=quint`k'r if pais==`k' & ur==2 & year==2012
qui replace wealthscores=f1`k'r if pais==`k' & ur==2 & year==2012

}

drop f1*u  f1*r quint*u quint*r wealthscores
* macro drop _all 
* drop f1`k'u f1`k'r quint`k'u quint`k'r wealthscores

qui gen wealthscores=.
qui levelsof pais if pais<40 & year==2014, local(K)
foreach k of local K {
qui pca   r1 r3 r4 r4a r5 r6 r7  r8 r12 r14 r15 r16  if pais==`k' & ur==1 & year==2014
qui predict  f1`k'u if pais==`k' & ur==1 & year==2014
qui sumdist f1`k'u,  n(5) qgp(quint`k'u)
qui replace quintall=quint`k'u if pais==`k' & ur==1 & year==2014
qui replace wealthscores=f1`k'u if pais==`k' & ur==1 & year==2014

qui pca   r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15 r16 if pais==`k' & ur==2 & year==2014
qui predict  f1`k'r if pais==`k' & ur==2 & year==2014
qui sumdist f1`k'r,  n(5) qgp(quint`k'r)
qui replace quintall=quint`k'r if pais==`k' & ur==2 & year==2014
qui replace wealthscores=f1`k'r if pais==`k' & ur==2 & year==2014

}

drop f1*u  f1*r quint*u quint*r wealthscores
* macro drop _all 
* drop f1`k'u f1`k'r quint`k'u quint`k'r wealthscores

qui gen wealthscores=.
qui levelsof pais if pais<40 & year==2016, local(K)
foreach k of local K {
qui pca   r1 r3 r4 r4a r5 r6 r7  r8 r12 r14 r15 r16  if pais==`k' & ur==1 & year==2016
qui predict  f1`k'u if pais==`k' & ur==1 & year==2016
qui sumdist f1`k'u,  n(5) qgp(quint`k'u)
qui replace quintall=quint`k'u if pais==`k' & ur==1 & year==2016
qui replace wealthscores=f1`k'u if pais==`k' & ur==1 & year==2016

qui pca   r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15 r16 if pais==`k' & ur==2 & year==2016
qui predict  f1`k'r if pais==`k' & ur==2 & year==2016
qui sumdist f1`k'r,  n(5) qgp(quint`k'r)
qui replace quintall=quint`k'r if pais==`k' & ur==2 & year==2016
qui replace wealthscores=f1`k'r if pais==`k' & ur==2 & year==2016

}

drop f1*u  f1*r quint*u quint*r wealthscores
* macro drop _all 
* drop f1`k'u f1`k'r quint`k'u quint`k'r wealthscores

qui gen wealthscores=.
qui levelsof pais if pais<40 & year==2017, local(K)
foreach k of local K {
qui pca   r1 r3 r4 r4a r5 r6 r7  r8 r12 r14 r15 r16  if pais==`k' & ur==1 & year==2017
qui predict  f1`k'u if pais==`k' & ur==1 & year==2017
qui sumdist f1`k'u,  n(5) qgp(quint`k'u)
qui replace quintall=quint`k'u if pais==`k' & ur==1 & year==2017
qui replace wealthscores=f1`k'u if pais==`k' & ur==1 & year==2017

qui pca   r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15 r16 if pais==`k' & ur==2 & year==2017
qui predict  f1`k'r if pais==`k' & ur==2 & year==2017
qui sumdist f1`k'r,  n(5) qgp(quint`k'r)
qui replace quintall=quint`k'r if pais==`k' & ur==2 & year==2017
qui replace wealthscores=f1`k'r if pais==`k' & ur==2 & year==2017

}


drop f1*u  f1*r quint*u quint*r wealthscores
* macro drop _all 
* drop f1`k'u f1`k'r quint`k'u quint`k'r wealthscores

qui gen wealthscores=.
qui levelsof pais if pais<40 & year==2018, local(K)
foreach k of local K {
qui pca   r1 r3 r4 r4a r5 r6 r7  r8 r12 r14 r15 r16  if pais==`k' & ur==1 & year==2018
qui predict  f1`k'u if pais==`k' & ur==1 & year==2018
qui sumdist f1`k'u,  n(5) qgp(quint`k'u)
qui replace quintall=quint`k'u if pais==`k' & ur==1 & year==2018
qui replace wealthscores=f1`k'u if pais==`k' & ur==1 & year==2018

qui pca   r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15 r16 if pais==`k' & ur==2 & year==2018
qui predict  f1`k'r if pais==`k' & ur==2 & year==2018
qui sumdist f1`k'r,  n(5) qgp(quint`k'r)
qui replace quintall=quint`k'r if pais==`k' & ur==2 & year==2018
qui replace wealthscores=f1`k'r if pais==`k' & ur==2 & year==2018

}

drop f1*u  f1*r quint*u quint*r wealthscores
* macro drop _all 
* drop f1`k'u f1`k'r quint`k'u quint`k'r wealthscores

qui gen wealthscores=.
qui levelsof pais if pais<40 & year==2019, local(K)
foreach k of local K {
qui pca   r1 r3 r4 r4a r5 r6 r7  r8 r12 r14 r15 r16  if pais==`k' & ur==1 & year==2019
qui predict  f1`k'u if pais==`k' & ur==1 & year==2019
qui sumdist f1`k'u,  n(5) qgp(quint`k'u)
qui replace quintall=quint`k'u if pais==`k' & ur==1 & year==2019
qui replace wealthscores=f1`k'u if pais==`k' & ur==1 & year==2019

qui pca   r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15 r16 if pais==`k' & ur==2 & year==2019
qui predict  f1`k'r if pais==`k' & ur==2 & year==2019
qui sumdist f1`k'r,  n(5) qgp(quint`k'r)
qui replace quintall=quint`k'r if pais==`k' & ur==2 & year==2019
qui replace wealthscores=f1`k'r if pais==`k' & ur==2 & year==2019

}

*******************************************************************************

gen poor = .
replace poor = 1 if quintall ==1
replace poor = 0 if quintall > 1
label var poor "poorest asset quintile"

gen poor2=.
replace poor2 = 1 if quintall<3
replace poor2 = 0 if quintall> 2
label var poor2 "poorest 40 % by assets" 

* create dummy variables for each category of quintall
gen quint1 = 1 if quintall==1
replace quint1 =0 if quintall~=1
label var quint1 "lowest asset quintile" 

gen quint2 = 1 if quintall==2
replace quint2 =0 if quintall~=2
label var quint2 "second lowest asset quintile" 

gen quint3 = 1 if quintall==3
replace quint3 = 0 if quintall~=3
label var quint3 "middle asset quintile" 

gen quint4 = 1 if quintall==4
replace quint4 =0 if quintall~=4
label var quint4 "second highest asset quintile"

gen quint5 = 1 if quintall ==5
replace quint5 = 0 if quintall~=5
label var quint5 "highest asset quintile" 

*** Quintall variable with value labels
gen wealth=quintall
label define wealth 1 "Poorest 20%" 5 "Wealthiest 20%", replace
label values wealth wealth
label var wealth "Wealth Quintiles"

// OTHER DEMOGRAPHIC AND ATTITUDINAL VARIABLES //

gen female=q1 
label variable female "gender"
recode female (2=1) (1=0)
label define female 1 "Mujer" 0 "Hombre" 
label val female female

gen age=q2
label variable age "age in years"

gen edu_years = ed 
label variable edu_years "years of education completed"

gen religiosity2=q5b 
label variable religiosity2 "importance of religion in one's life"
recode religiosity2 (1=4) (2=3) (3=2) (4=1) 
label define religiosity2 1 "nada importante" 2 "poco importante" 3 "algo importante" 4 "muy importante"

gen ethnicity=etid 
label variable ethnicity "respondent's ethnicity"

gen indig= ethnicity 
replace indig=1 if ethnicity==3
replace indig=1 if ethnicity==1110
replace indig=1 if ethnicity==1111
replace indig=0 if ethnicity==1
replace indig=0 if ethnicity==2
replace indig=0 if ethnicity==4
replace indig=0 if ethnicity==5
replace indig=0 if ethnicity==7
replace indig=0 if ethnicity==1106
replace indig=0 if ethnicity==1112
replace indig=0 if ethnicity==1113
replace indig=0 if ethnicity==1506
replace indig=0 if ethnicity==1606
replace indig = . if ethnicity== .c
replace indig = . if ethnicity== .a
label var indig "Indigenous"
label define indig 1 "Indigenous" 0 "Not Indigenous" 
label val indig indig

gen parentslang_lax=leng4
replace parentslang_lax=1 if leng4==2 | leng4==3
replace parentslang_lax=0 if leng4==1 | leng4==4 | leng4==5
label var parentslang_lax "Parents' language"
label define parentslang_lax 1 "Parents spoke indig.lang. or spanish" 0 "Other"
label val parentslang_lax parentslang_lax

gen rural=ur
label variable rural "rural"
recode rural (2=1) (1=0)
label define rural 1 "Rural" 0 "Urban" 
label val rural rural

gen trust= it1 
label variable trust "interpersonal trust"
recode trust (1=4) (2=3) (3=2) (4=1)
label define trust 1 "Nada confiable" 2 "Poco confiable" 3 "Algo confiable" 4 "Mucho confiable"
label val trust trust
	
gen polinterest=pol1
recode polinterest (4 = 1) (3 = 2) (2 = 3) (1 = 4)
label var polinterest "How interested are you in politics (pol1)"
label define polinterest 1 "Not at all" 2 "A little" 3 "Some" 4 "A lot"
la val polinterest polinterest

// ADDING COUNTRY-LEVEL VARIABLES //

* See file "cntr_lvl_vars.dta"

* Sources: 
	* Indigenous population size (in %): CEPAL (https://www.cepal.org/en/infografias/los-pueblos-indigenas-en-america-latina)
	* Presence of an Indigenous party: Rice & Van Cott 2006 (https://journals.sagepub.com/doi/abs/10.1177/0010414005285036?journalCode=cpsa)
	* Existence of compulsory voting in a country: Database of Political Institutions (https://datacatalog.worldbank.org/dataset/wps2283-database-political-institutions)
	
joinby pais using "cntr_lvl_vars.dta"

* Save in a desired location.
